package xyz.mumuwei.youthcourseserver.controller;


import com.alipay.api.AlipayApiException;
import lombok.RequiredArgsConstructor;
import org.quartz.SchedulerException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.mumuwei.youthcourseserver.common.dto.PageDto;
import xyz.mumuwei.youthcourseserver.common.lang.Result;
import xyz.mumuwei.youthcourseserver.pojo.Order;
import xyz.mumuwei.youthcourseserver.pojo.UserInfo;
import xyz.mumuwei.youthcourseserver.service.OrderService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;

/**
 * @author mumuwei
 * @since 2022-02-19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/order")
public class OrderController {

    final OrderService orderService;

    @GetMapping("/page")
    public Result getOrderList(PageDto pageDto) {
        return orderService.getOrderPage(pageDto);
    }

    @GetMapping("/pages")
    public Result getOrderByStudent(PageDto pageDto, @RequestAttribute Integer uid) {
        pageDto.setId(uid);
        return orderService.getByStudentId(pageDto);
    }

    @GetMapping("/list")
    public Result getOrderList() {
        return orderService.getOrderList();
    }

    /**
     * 管理员 web端创建支付请求
     * @param order 订单信息
     * @return 响应结果
     * @throws AlipayApiException 支付宝异常
     */
    @PostMapping("/create")
    public Result createOrder(@Validated Order order) throws AlipayApiException, SchedulerException {
        return orderService.createOrder(order);
    }

    /**
     * 移动端创建订单
     * @param order 订单信息
     * @return 响应结果
     * @throws AlipayApiException 支付宝异常
     */
    @PostMapping("/wapCreate")
    public Result createWapOrder(Order order, @RequestAttribute Integer uid) throws AlipayApiException, SchedulerException {
        if (order.getCourseId() != null && order.getPayType() != null) {
            return orderService.createByStudent(order, uid, false);
        }
        return Result.fail("请求参数有误！");
    }

    /**
     * 确认信息，创建手机支付宝订单
     * @param orderId 订单id
     * @return 响应结果
     */
    @PostMapping("/gotoWapAlipay")
    public Result goWapAlipay(String orderId) throws AlipayApiException {
        return orderService.gotoWapAlipay(orderId);
    }

    @PostMapping("/create/student")
    public Result createByStudent(Order order, @RequestAttribute Integer uid) throws AlipayApiException, SchedulerException {
        if (order.getCourseId() != null && order.getPayType() != null) {
            return orderService.createByStudent(order, uid, true);
        }
        return Result.fail("请求参数有误！");
    }

    @PostMapping("/update")
    public Result updateOrderStudent(@Validated UserInfo info) {
        return orderService.updateStudent(info);
    }

    @GetMapping("/del_{code}")
    public Result deleteByOrderId(@PathVariable String code) {
        return orderService.deleteByOrderId(code);
    }

    @GetMapping("/{code}")
    public Result getOrderByCode(@PathVariable String code) {
        return orderService.getOrderByCode(code);
    }

    /**
     * PC端支付成功 同步参数返回
     */
    @GetMapping("/pcAlipayReturn")
    public void alipayReturnPc(HttpServletRequest req, HttpServletResponse res) throws IOException, AlipayApiException {
        String outTradeNo = req.getParameter("out_trade_no");
        orderService.queryOrderInAlipayByCode(outTradeNo);
        res.sendRedirect("http://localhost:9527/#/order/info?code="+outTradeNo);
    }

    /**
     * 支付宝异步通知
     */
    @PostMapping("/alipayNotify")
    public void alipayReturn(HttpServletRequest request) throws AlipayApiException, ParseException {
        orderService.alipayNotifyHandle(request);
    }


    /**
     * 根据订单编号取消顶顶那
     */
    @GetMapping("/cancel/{code}")
    public Result cancelOrder(@PathVariable String code) throws SchedulerException {
        return orderService.cancelOrderByCode(code);
    }
}

